#
#	File:		BBSStartup
#
#	Written by:	Kurt Clark
#
#	Copyright:	 1993 by Apple Computer, Inc., all rights reserved.
#
#	Change History (most recent first):
#
#	   <SM6>	10/13/93	IH		Check shell variable BlockSuperMarioSetKeys and do not call
#									setkeys if it is set (I was already using these keys).
#	   <SM5>	 6/14/93	kc		Fix some quoting problems
#	   <SM4>	 5/21/93	kc		Move UseToolServer check to after the control keys are set, fix
#									lookup entrypoint so that it will work with Ludwig sources.
#	   <SM3>	  3/3/93	kc		Fix toolserver so that it isn't launched by default.
#	   <SM2>	 2/24/93	kc		Change default tools server worksheet to TSWorkSheet.
#	   <SM1>	2/19/93		kc		Rewrite existing BBSStartup.
#
#

#
#	Variables
#	
	Set TheNameFile "{Sources}SuperMarioSources:Names"
	Export TheNameFile

	If	!"{ToolServer}"
		Set ToolServer "ToolServer"
		Export ToolServer
	End
	
	If	!{TSWorkSheet}
		Set TSWorkSheet "{MPW}TSWorkSheet"
		Export TSWorkSheet
	End


#
#	Aliases	
#	

	Alias	Nothing		"Set Status 0"					# For convenience.
	
	Alias	ToolServer	"RShell -r "{ToolServer}"  "{TSWorkSheet}""

#
#	Keys	
#
#
# Give a chance to block these keys if we don't want them.
#
	If !{BlockSuperMarioSetKeys}

		# Make sure the MPW Shell supports SetKeys.
		Which SetKey  dev:null								
			||	Begin										
					Which SetKeys  dev:null				
					&&	Begin								
						Alias SetKey SetKeys 				
						End									
					||	Begin								
						Alias SetKey '#' 				
						End									
				End
	
	
			# Execute in background.
			SetKey	Control-Return											 
					   '(Evaluate "`Position "{active}"`" =~				 
							/([0-9]*)1 ([0-9]+)2,([0-9]+)3/ )  Dev:Null	;
						If {2} == {3}	; Find {1} "{active}" ; End		;
						ToolServer < "{active}". 							;
						Find	!{2}:!{3}	 "{active}"					'
						
			# Kill running background task.
			SetKey	Control-.												 
					   'Set pid 0											;
						Loop												;
							RShell -k {pid} && Break ; || Nothing			;
							Evaluate pid += 1								;
							Break If {pid} >= 16							;
						End  Dev:Null										'
		
			# Kill all background tasks.
			SetKey	Control-Shift-.											
					   'Set pid 0											;
						Loop												;
							RShell -k {pid} || Nothing						;
							Evaluate pid += 1								;
							Break If {pid} >= 16							;
						End  Dev:Null										'
		
			# Locate the source for the selected entrypoint.
			SetKey	Command-Option-D '"{sources}"Tools:LookupEntryPoint'

	End
#
#	Check Switch
#

	If	!{UseToolServer}
		Exit 0
	End

#
#	Patch the shell so that you can issue multiple commands
#

	{sources}Tools:PatchShell

#
#	Launch the ToolServer if running local, or locate it otherwise.
#

	Open -n -t "{TSWorkSheet}"
	
	If	"{ToolServer}" =~ /"ToolServer"/					# running local
		If	`Exists "{MPW}ToolServer"`
			Execute "{MPW}ToolServer"   			
				||	Begin
						Alert "I couldnt launch tool server."
						Exit 0
					End
			ToolServer		'"{MPW}MPWShell"'
		Else
			Exit 0
		End
	Else
		ToolServer 'Nothing'  Dev:Null  			
			||	Begin
					Alert "I couldnt locate your tool server."
					Exit 0
				End
	End

#
#	Initialize ToolServer
#
	
	Set 	|	ToolServer
	Export	|	ToolServer
	Alias	|	ToolServer


#
#	Done
#

	Exit 0

#
#	End of BBSStartup
#
